{% extends 'app_doc/user/user_base.html' %}
{% load static %}
{% block title %}文档管理{% endblock %}
{% block content %}
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <form action="{% url 'manage_doc' %}" method="get">
                <input name="pid" value="{{docs.pid}}" hidden>
                <div class="layui-form-item">
                    <span class="doc_status_condition" lay-separator="|">
                    <a href="{% url 'manage_doc' %}?status=all" class="{% if doc_status == 'all' %}current{% endif %}">全部（{{all_cnt}}）</a>
                    <a href="{% url 'manage_doc' %}?status=published" class="{% if doc_status == 'published' %}current{% endif %}">已发布（{{published_doc_cnt}}）</a>
                    <a href="{% url 'manage_doc' %}?status=draft" class="{% if doc_status == 'draft' %}current{% endif %}">草稿（{{draft_doc_cnt}}）</a>
                    <input name="status" value="{% if doc_status == 'all' %}all{% elif doc_status == 'published' %}published{% elif doc_status == 'draft' %}draft{% endif %}" hidden>
                    </span>
                    <div class="layui-input-inline" style="float:inherit;">
                        <input type="text" name="kw" id="kw" placeholder="输入文档标题或内容" autocomplete="off" class="layui-input">
                    </div>
                    <button class="layui-btn layui-btn-normal layui-btn-xs" type="submit"><i class="layui-icon layui-icon-search"></i>搜索</button>
                    <a href="{% url 'create_doc' %}" target="_blank" class="layui-btn layui-btn-primary layui-btn-xs"><i class="layui-icon layui-icon-addition"></i>新建</a>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="">
            <div class="layui-form">
                <div class="layui-input-inline">
                <select name="pro_id" lay-verify="required"  lay-filter="project" id="project">
                    <option value="">按文集筛选</option>
                    <!-- 自己的文集 -->
                    <optgroup label="自有文集" id="self-project">
                    {% for p in project_list %}
                        {% if p.role == 1 %}
                        <option value="{{ p.id }}">[私密]《{{ p.name }}》</option>
                        {% elif p.role == 2 %}
                        <option value="{{ p.id }}" >[指定用户]《{{ p.name }}》</option>
                        {% elif p.role == 3 %}
                        <option value="{{ p.id }}" >[访问码]《{{ p.name }}》</option>
                        {% else %}
                        <option value="{{ p.id }}" >[公开]《{{ p.name }}》</option>
                        {% endif %}
                    {% endfor %}
                    </optgroup>
                    <!-- 协作的文集 -->
                    {% if colla_project_list.count > 0 %}
                    <optgroup label="协作文集">
                    {% for p in colla_project_list %}
                            <option value="{{ p.project.id }}">[协作]《{{ p.project.name }}》</option>
                    {% endfor %}
                    </optgroup>
                    {% endif %}
                </select>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row" lay-skin="">
            <table class="layui-table"  id="doctemp-list" lay-skin="nob" lay-even>
        <thead>
            <tr>
                <th>文档名称</th>
                <th>上级文档</th>
                <th>展开下级</th>
                <th>所属文集</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% load doc_filter %}
            {% for doc in docs %}
            <tr>
                {% if doc.status == 1 %}
                    <td>
                        <img src="{% static 'icon_img/manage-doc-icon.svg' %}" height="20px" width="20px" />
                        <a href="{% url 'doc' doc.top_doc doc.id %}" target="_blank" title="查看文档：{{doc.name}}">{{ doc.name }} 
                        </a>
                    </td>
                {% else %}
                    <td>
                        <img src="{% static 'icon_img/manage-doc-pre-icon.svg' %}" height="20px" width="20px" />
                        <a href="{% url 'modify_doc' doc.id %}" target="_blank" title="修改文档：{{doc.name}}">{{ doc.name }} 
                        </a>
                        <button class="layui-btn layui-btn-xs layui-btn-normal" onclick="fastPubDoc('{{doc.id}}')">一键发布</button>
                    </td>
                {% endif %}
                <td>{{ doc.parent_doc|get_doc_parent }}</td>
                <td>{{ doc.open_children }}</td>
                <td>
                    {{ doc.top_doc|is_colla_pro:doc.create_user }}{{ doc.top_doc|get_doc_top }}
                </td>
                <td>{{ doc.create_time }}</td>
                <td>
                    <a href="{% url 'manage_doc_history' doc.id %}" title="查看文档历史" class="layui-btn layui-btn-xs layui-btn-normal">
                        <i class="layui-icon layui-icon-log"></i>
                    </a>
                    <a href="{% url 'modify_doc' doc_id=doc.id %}" title="修改文档" target="_blank" class="layui-btn layui-btn-xs layui-btn-normal">
                        <i class="layui-icon layui-icon-edit"></i>
                    </a>
                    <a href="javascript:void(0);" title="删除文档" onclick="delDoc('{{doc.id}}');" class="layui-btn layui-btn-xs layui-btn-warm">
                        <i class="layui-icon layui-icon-delete"></i>
                    </a>
                </td>
            </tr>
            {% endfor %}
        </tbody>
            </table>
        </div>
        <!-- 分页 -->
        <div class="layui-row">
            <div class="layui-box layui-laypage layui-laypage-default">
                    <!-- 上一页 -->
                    {% if docs.has_previous %}
                        <a href="?page={{ docs.previous_page_number }}&kw={{docs.kw}}&status={{docs.status}}&pid={{docs.pid}}" class="layui-btn layui-btn-xs layui-btn-normal">上一页</a>
                    {% else %}
                        <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-disabled">上一页</a>
                    {% endif %}
                    <!-- 当前页 -->
                    <span class="layui-laypage-curr">
                        <em class="layui-laypage-em"></em>
                        <em>{{ docs.number }}/{{ docs.paginator.num_pages }}</em>
                    </span>
                    <!-- 下一页 -->
                    {% if docs.has_next %}
                        <a href="?page={{ docs.next_page_number }}&kw={{docs.kw}}&status={{docs.status}}&pid={{docs.pid}}" class="layui-btn layui-btn-xs layui-btn-normal">下一页</a>
                    {% else %}
                        <a class="layui-btn layui-btn-xs layui-btn-disabled">下一页</a>
                    {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block custom_script %}
<script>
    layui.use(['table', 'form', 'jquery', 'dtree','layer'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        var layer = layui.layer;
        $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}' },
        });
        // 删除文档
        delDoc = function(doc_id){
            layer.open({
                type:1,
                title:'删除文档',
                area:'300px;',
                id:'delPro',//配置ID
                content:'<div style="margin-left:10px;">警告：此操作将删除此文档！</div>',
                btn:['确定','取消'], //添加按钮
                btnAlign:'c', //按钮居中
                yes:function (index,layero) {
                    layer.load(1);
                    data = {
                        'doc_id':doc_id,
                    }
                    $.post("{% url 'del_doc' %}",data,function(r){
                        layer.closeAll('loading')
                        if(r.status){
                            //修改成功
                            window.location.reload();
                            //layer.close(index)
                        }else{
                            //修改失败，提示
                            console.log(r)
                            layer.msg(r.data)
                        }
                    })
                },
            });
        };
        // 一键发布文档
        fastPubDoc = function(doc_id){
            data = {
                'doc_id':doc_id,
            }
            $.post("{% url 'fast_pub_doc' %}",data,function(r){
                layer.closeAll('loading')
                if(r.status){
                    //发布成功
                    window.location.reload();
                }else{
                    //发布失败，提示
                    console.log(r)
                    layer.msg(r.data)
                }
            })
        };
        //筛选文集
        form.on('select(project)', function(data){
            console.log('选择文集：',data.value); //得到被选中的值
            if(data.value != -1){//
                window.location.href = '?kw={{docs.kw}}&status={{docs.status}}&pid='+data.value;
            }
        });
    })
</script>
{% endblock %}